home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / nethack.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  5.8 KB  |  255 lines

  1. /*
  2.  * network kernel hackin' on a FreeBSD box....
  3.  *
  4.  * why not?
  5.  *
  6.  * yeah it's possible ... this is an example ... you can change hack_input()
  7.  * function as you wanna...
  8.  *
  9.  * We can change functions of a struct of inetsw[], we can change mbuf
  10.  * structures... we can access inpcb,inpcbinfo structures... we can change
  11.  * options of every layer in a connection...
  12.  *
  13.  * idea & code by pigpen [deadhead@sikurezza.org, pigpen@s0ftpj.org]
  14.  *
  15.  */
  16.  
  17. /*
  18.  * uname -a
  19.  *
  20.  * FreeBSD storpio.cameretta.pig 4.0-19990705-CURRENT FreeBSD 4.0-19990705-
  21.  * CURRENT #4 ..... i386
  22.  *
  23.  */
  24.  
  25.  
  26. #include <sys/param.h>
  27. #include <sys/systm.h>
  28. #include <sys/malloc.h>
  29. #include <sys/mbuf.h>
  30. #include <sys/kernel.h>
  31. #include <sys/proc.h>
  32. #include <sys/socket.h>
  33. #include <sys/socketvar.h>
  34. #include <sys/sysctl.h>
  35. #include <sys/syslog.h>
  36. #include <sys/protosw.h>
  37. #include <net/if.h>
  38. #include <net/route.h>
  39. #include <netinet/in.h>
  40. #include <netinet/in_systm.h>
  41. #include <netinet/in_pcb.h>
  42. #include <netinet/ip.h>
  43. #include <netinet/ip_var.h>
  44. #include <netinet/ip_icmp.h>
  45. #include <netinet/udp.h>
  46. #include <netinet/udp_var.h>
  47.  
  48.  
  49. extern struct     protosw     inetsw[];
  50. extern struct     udpstat     udpstat;
  51. extern int      log_in_vain;
  52. extern struct     inpcbhead     udb;
  53. extern struct     sockaddr_in     udp_in;
  54.  
  55. static void     (*old_udp_input)    __P((register struct mbuf *, int ));
  56. static void      hack_input        __P((register struct mbuf *, int ));
  57. static int      s_load             __P((struct module *, int, void *));
  58.  
  59. static int
  60. s_load (struct module *module, int cmd, void *arg)
  61. {
  62.  int s;
  63.  
  64.  switch(cmd) {
  65.     case MOD_LOAD:
  66.             printf("Module loaded");
  67.             s = splnet();
  68.               old_udp_input = inetsw[1].pr_input;
  69.             inetsw[1].pr_input = hack_input;
  70.                 splx(s);
  71.             break;
  72.  
  73.     case MOD_UNLOAD:
  74.             s = splnet();
  75.             inetsw[1].pr_input = old_udp_input;
  76.             splx(s);
  77.             break;
  78.  }
  79.  
  80. return 0;
  81. }
  82.  
  83. static moduledata_t s_mod_1 = {
  84.             "s_mod",
  85.             s_load,
  86.             0
  87. };
  88.  
  89. DECLARE_MODULE(s_mod, s_mod_1, SI_SUB_PSEUDO, SI_ORDER_ANY);
  90.  
  91. static void
  92. hack_input(m, iphlen)
  93.     register struct mbuf *m;
  94.     int iphlen;
  95. {
  96.     register struct ip *ip;
  97.     register struct udphdr *uh;
  98.     register struct inpcb *inp;
  99.     struct mbuf *opts = 0;
  100.     int len;
  101.     struct ip save_ip;
  102.  
  103.     udpstat.udps_ipackets++;
  104.  
  105.     if (iphlen > sizeof (struct ip)) {
  106.         ip_stripoptions(m, (struct mbuf *)0);
  107.         iphlen = sizeof(struct ip);
  108.     }
  109.  
  110.     ip = mtod(m, struct ip *);
  111.     if (m->m_len < iphlen + sizeof(struct udphdr)) {
  112.         if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
  113.             udpstat.udps_hdrops++;
  114.             return;
  115.         }
  116.         ip = mtod(m, struct ip *);
  117.     }
  118.     uh = (struct udphdr *)((caddr_t)ip + iphlen);
  119.  
  120.     len = ntohs((u_short)uh->uh_ulen);
  121.     if (ip->ip_len != len) {
  122.         if (len > ip->ip_len || len < sizeof(struct udphdr)) {
  123.             udpstat.udps_badlen++;
  124.             goto bad;
  125.         }
  126.         m_adj(m, len - ip->ip_len);
  127.         /* ip->ip_len = len; */
  128.     }
  129.     save_ip = *ip;
  130.  
  131.     if (uh->uh_sum) {
  132.         bzero(((struct ipovly *)ip)->ih_x1, 9);
  133.         ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
  134.         uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
  135.         if (uh->uh_sum) {
  136.             udpstat.udps_badsum++;
  137.             m_freem(m);
  138.             return;
  139.         }
  140.     }
  141.  
  142.     if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
  143.         in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
  144.         struct inpcb *last;
  145.  
  146.         udp_in.sin_port = uh->uh_sport;
  147.         udp_in.sin_addr = ip->ip_src;
  148.         m->m_len -= sizeof (struct udpiphdr);
  149.         m->m_data += sizeof (struct udpiphdr);
  150.  
  151.         last = NULL;
  152.         for (inp = udb.lh_first; inp != NULL; inp = inp->inp_list.le_next) {
  153.             if (inp->inp_lport != uh->uh_dport)
  154.                 continue;
  155.             if (inp->inp_laddr.s_addr != INADDR_ANY) {
  156.                 if (inp->inp_laddr.s_addr !=
  157.                     ip->ip_dst.s_addr)
  158.                     continue;
  159.             }
  160.             if (inp->inp_faddr.s_addr != INADDR_ANY) {
  161.                 if (inp->inp_faddr.s_addr !=
  162.                     ip->ip_src.s_addr ||
  163.                     inp->inp_fport != uh->uh_sport)
  164.                     continue;
  165.             }
  166.  
  167.             if (last != NULL) {
  168.                 struct mbuf *n;
  169.  
  170.                 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
  171.                     if (last->inp_flags & INP_CONTROLOPTS
  172.                         || last->inp_socket->so_options & SO_TIMESTAMP)
  173.                         ip_savecontrol(last, &opts, ip, n);
  174.                     if (sbappendaddr(&last->inp_socket->so_rcv,
  175.                         (struct sockaddr *)&udp_in,
  176.                         n, opts) == 0) {
  177.                         m_freem(n);
  178.                         if (opts)
  179.                             m_freem(opts);
  180.                         udpstat.udps_fullsock++;
  181.                     } else
  182.                         sorwakeup(last->inp_socket);
  183.                     opts = 0;
  184.                 }
  185.             }
  186.             last = inp;
  187.             if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
  188.                 break;
  189.         }
  190.  
  191.         if (last == NULL) {
  192.             udpstat.udps_noportbcast++;
  193.             goto bad;
  194.         }
  195.         if (last->inp_flags & INP_CONTROLOPTS
  196.             || last->inp_socket->so_options & SO_TIMESTAMP)
  197.             ip_savecontrol(last, &opts, ip, m);
  198.         if (sbappendaddr(&last->inp_socket->so_rcv,
  199.              (struct sockaddr *)&udp_in,
  200.              m, opts) == 0) {
  201.             udpstat.udps_fullsock++;
  202.             goto bad;
  203.         }
  204.         sorwakeup(last->inp_socket);
  205.         return;
  206.     }
  207.     inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
  208.         ip->ip_dst, uh->uh_dport, 1);
  209.     if (inp == NULL) {
  210.         if (log_in_vain) {
  211.             char buf[4*sizeof "123"];
  212.  
  213.             strcpy(buf, inet_ntoa(ip->ip_dst));
  214.             log(LOG_INFO,
  215.                 "Connection attempt to UDP %s:%d from %s:%d\n",
  216.                 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
  217.                 ntohs(uh->uh_sport));
  218.         }
  219.         udpstat.udps_noport++;
  220.         if (m->m_flags & (M_BCAST | M_MCAST)) {
  221.             udpstat.udps_noportbcast++;
  222.             goto bad;
  223.         }
  224.         *ip = save_ip;
  225.  
  226.         if (badport_bandlim(0) < 0)
  227.             goto bad;
  228.  
  229.         icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
  230.         return;
  231.     }
  232.  
  233.     udp_in.sin_port = uh->uh_sport;
  234.     udp_in.sin_addr = ip->ip_src;
  235.     if (inp->inp_flags & INP_CONTROLOPTS
  236.         || inp->inp_socket->so_options & SO_TIMESTAMP)
  237.         ip_savecontrol(inp, &opts, ip, m);
  238.     iphlen += sizeof(struct udphdr);
  239.     m->m_len -= iphlen;
  240.     m->m_pkthdr.len -= iphlen;
  241.     m->m_data += iphlen;
  242.     if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in,
  243.         m, opts) == 0) {
  244.         udpstat.udps_fullsock++;
  245.         goto bad;
  246.     }
  247.     sorwakeup(inp->inp_socket);
  248.     return;
  249. bad:
  250.     m_freem(m);
  251.     if (opts)
  252.         m_freem(opts);
  253. }
  254.  
  255.